<!DOCTYPE html><html lang="en"><head><title>payments/servlet/PaymentWithSavedCardServlet</title></head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="groc-relative-root" content="../../"><meta name="groc-document-path" content="payments/servlet/PaymentWithSavedCardServlet"><meta name="groc-project-path" content="src/main/java/com/paypal/api/payments/servlet/PaymentWithSavedCardServlet.java"><link rel="stylesheet" type="text/css" media="all" href="../../assets/style.css"><script type="text/javascript" src="../../assets/behavior.js"></script><body><div id="meta"><div class="file-path">src/main/java/com/paypal/api/payments/servlet/PaymentWithSavedCardServlet.java</div></div><div id="document"><div class="segment"><div class="comments "><div class="wrapper"><h1 id="createpayment-using-saved-card-sample">CreatePayment Using Saved Card Sample</h1>
<p>This sample code demonstrates how you can process a 
Payment using a previously saved credit card.
API used: /v1/payments/payment</p></div></div><div class="code"><div class="wrapper"><span class="hljs-keyword">package</span> com.paypal.api.payments.servlet;

<span class="hljs-keyword">import</span> java.io.IOException;
<span class="hljs-keyword">import</span> java.io.InputStream;
<span class="hljs-keyword">import</span> java.util.*;

<span class="hljs-keyword">import</span> javax.servlet.*;
<span class="hljs-keyword">import</span> javax.servlet.http.*;

<span class="hljs-keyword">import</span> org.apache.log4j.Logger;

<span class="hljs-keyword">import</span> com.paypal.api.payments.*;
<span class="hljs-keyword">import</span> com.paypal.api.payments.util.*;
<span class="hljs-keyword">import</span> com.paypal.base.rest.APIContext;
<span class="hljs-keyword">import</span> com.paypal.base.rest.PayPalRESTException;
<span class="hljs-keyword">import</span> com.paypal.base.rest.PayPalResource;</div></div></div><div class="segment"><div class="comments doc-section"><div class="wrapper"><p><span class='doc-section-header'> author lvairamani</span></p></div></div><div class="code"><div class="wrapper"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PaymentWithSavedCardServlet</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">HttpServlet</span> </span>{

	<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> serialVersionUID = <span class="hljs-number">1L</span>;

	<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> Logger LOGGER = Logger
			.getLogger(PaymentWithSavedCardServlet.class);

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span><span class="hljs-params">(ServletConfig servletConfig)</span> <span class="hljs-keyword">throws</span> ServletException </span>{</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h2 id="load-configuration">Load Configuration</h2>
<p>Load SDK configuration for
the resource. This intialization code can be
done as Init Servlet.</p></div></div><div class="code"><div class="wrapper">		InputStream is = PaymentWithSavedCardServlet.class
				.getResourceAsStream(<span class="hljs-string">"/sdk_config.properties"</span>);

		<span class="hljs-keyword">try</span> {
			PayPalResource.initConfig(is);
		} <span class="hljs-keyword">catch</span> (PayPalRESTException e) {
			LOGGER.error(e.getMessage());
		}

	}

	<span class="hljs-annotation">@Override</span>
	<span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doGet</span><span class="hljs-params">(HttpServletRequest req, HttpServletResponse resp)</span>
			<span class="hljs-keyword">throws</span> ServletException, IOException </span>{
		doPost(req, resp);
	}</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h2 id="create">Create</h2>
<p>Sample showing to create a Payment using
Saved CreditCard as a FundingInstrument</p></div></div><div class="code"><div class="wrapper">	<span class="hljs-annotation">@Override</span>
	<span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doPost</span><span class="hljs-params">(HttpServletRequest req, HttpServletResponse resp)</span>
			<span class="hljs-keyword">throws</span> ServletException, IOException </span>{</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="creditcard">CreditCard</h3>
<p>A resource representing a credit card that can be
used to fund a payment.</p></div></div><div class="code"><div class="wrapper">		CreditCardToken creditCardToken = <span class="hljs-keyword">new</span> CreditCardToken();
		creditCardToken.setCreditCardId(<span class="hljs-string">"CARD-5BT058015C739554AKE2GCEI"</span>);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="details">Details</h3>
<p>Let&#39;s you specify details of a payment amount.</p></div></div><div class="code"><div class="wrapper">		Details details = <span class="hljs-keyword">new</span> Details();
		details.setShipping(<span class="hljs-string">"1"</span>);
		details.setSubtotal(<span class="hljs-string">"5"</span>);
		details.setTax(<span class="hljs-string">"1"</span>);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="amount">Amount</h3>
<p>Let&#39;s you specify a payment amount.</p></div></div><div class="code"><div class="wrapper">		Amount amount = <span class="hljs-keyword">new</span> Amount();
		amount.setCurrency(<span class="hljs-string">"USD"</span>);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Total must be equal to the sum of shipping, tax and subtotal.</p></div></div><div class="code"><div class="wrapper">		amount.setTotal(<span class="hljs-string">"7"</span>);
		amount.setDetails(details);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="transaction">Transaction</h3>
<p>A transaction defines the contract of a
payment - what is the payment for and who
is fulfilling it. Transaction is created with
a <code>Payee</code> and <code>Amount</code> types</p></div></div><div class="code"><div class="wrapper">		Transaction transaction = <span class="hljs-keyword">new</span> Transaction();
		transaction.setAmount(amount);
		transaction
				.setDescription(<span class="hljs-string">"This is the payment transaction description."</span>);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>The Payment creation API requires a list of
Transaction; add the created <code>Transaction</code>
to a List</p></div></div><div class="code"><div class="wrapper">		List&lt;Transaction&gt; transactions = <span class="hljs-keyword">new</span> ArrayList&lt;Transaction&gt;();
		transactions.add(transaction);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="fundinginstrument">FundingInstrument</h3>
<p>A resource representing a Payeer&#39;s funding instrument.
In this case, a Saved Credit Card can be passed to
charge the payment.</p></div></div><div class="code"><div class="wrapper">		FundingInstrument fundingInstrument = <span class="hljs-keyword">new</span> FundingInstrument();
		fundingInstrument.setCreditCardToken(creditCardToken);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>The Payment creation API requires a list of
FundingInstrument; add the created <code>FundingInstrument</code>
to a List</p></div></div><div class="code"><div class="wrapper">		List&lt;FundingInstrument&gt; fundingInstrumentList = <span class="hljs-keyword">new</span> ArrayList&lt;FundingInstrument&gt;();
		fundingInstrumentList.add(fundingInstrument);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="payer">Payer</h3>
<p>A resource representing a Payer that funds a payment
Use the List of <code>FundingInstrument</code> and the Payment Method
as &#39;credit_card&#39;</p></div></div><div class="code"><div class="wrapper">		Payer payer = <span class="hljs-keyword">new</span> Payer();
		payer.setFundingInstruments(fundingInstrumentList);
		payer.setPaymentMethod(<span class="hljs-string">"credit_card"</span>);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="payment">Payment</h3>
<p>A Payment Resource; create one using
the above types and intent as &#39;sale&#39;</p></div></div><div class="code"><div class="wrapper">		Payment payment = <span class="hljs-keyword">new</span> Payment();
		payment.setIntent(<span class="hljs-string">"sale"</span>);
		payment.setPayer(payer);
		payment.setTransactions(transactions);

		<span class="hljs-keyword">try</span> {</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="accesstoken">AccessToken</h3>
<p>Retrieve the access token from
OAuthTokenCredential by passing in
ClientID and ClientSecret
It is not mandatory to generate Access Token on a per call basis.
Typically the access token can be generated once and
reused within the expiry window</p></div></div><div class="code"><div class="wrapper">			String accessToken = GenerateAccessToken.getAccessToken();</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="apicontext">APIContext</h3>
<p>APIContext which takes &#39;Access Token&#39;
argument</p></div></div><div class="code"><div class="wrapper">			APIContext apiContext = <span class="hljs-keyword">new</span> APIContext(accessToken);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Use this variant if you want to pass in a request id<br>that is meaningful in your application, ideally 
a order id.
String requestId = Long.toString(System.nanoTime();
APIContext apiContext = new APIContext(accessToken, requestId ));</p></div></div><div class="code"><div class="wrapper">			</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Create a payment by posting to the APIService
using a valid AccessToken
The return object contains the status;</p></div></div><div class="code"><div class="wrapper">			Payment createdPayment = payment.create(apiContext);
			LOGGER.info(<span class="hljs-string">"Created payment with id = "</span> + createdPayment.getId()
					+ <span class="hljs-string">" and status = "</span> + createdPayment.getState());
			ResultPrinter.addResult(req, resp, <span class="hljs-string">"Payment with Saved Card"</span>, Payment.getLastRequest(), Payment.getLastResponse(), <span class="hljs-keyword">null</span>);
		} <span class="hljs-keyword">catch</span> (PayPalRESTException e) {
			ResultPrinter.addResult(req, resp, <span class="hljs-string">"Payment with Saved Card"</span>, Payment.getLastRequest(), <span class="hljs-keyword">null</span>, e.getMessage());
		}
		
		req.getRequestDispatcher(<span class="hljs-string">"response.jsp"</span>).forward(req, resp);
	}
}</div></div></div></div></body></html>